package algorithm.digui;

import com.alibaba.fastjson.JSON;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2020/8/20
 * @Version：1.0.0
 */
public class MenuTreeTest {

    private static final Logger logger = LoggerFactory.getLogger(MenuTreeTest.class);


    static List<Menu> list = new ArrayList<>();

    static {

        Menu menu = new Menu();
        menu.setMenuid("0001");
        menu.setName("根节点");
        menu.setParentid("0000");

        Menu menu1 = new Menu();
        menu1.setMenuid("0002");
        menu1.setName("节点1");
        menu1.setParentid("0001");

        Menu menu2 = new Menu();
        menu2.setMenuid("0003");
        menu2.setName("节点2");
        menu2.setParentid("0001");

        Menu menu3 = new Menu();
        menu3.setMenuid("0004");
        menu3.setName("节点3");
        menu3.setParentid("0002");

        Menu menu4 = new Menu();
        menu4.setMenuid("0005");
        menu4.setName("节点4");
        menu4.setParentid("0003");

        Menu menu5 = new Menu();
        menu5.setMenuid("0006");
        menu5.setName("节点5");
        menu5.setParentid("0005");
        list.add(menu);
        list.add(menu1);
        list.add(menu2);
        list.add(menu3);
        list.add(menu4);
        list.add(menu5);
    }


    public static void main(String[] args) {
        for (Menu menu : list) {
            logger.info("{}", menu);
        }
        logger.info("=============================");
        // 得到第一个节点，并且删除
        Iterator<Menu> iterator = list.iterator();

        List<TreeItem> treeItems = new ArrayList<>();
        TreeItem treeItem = new TreeItem();
        String rootNode = "";
        while (iterator.hasNext()) {
            Menu next = iterator.next();
            if ("0001".equals(next.getMenuid())) {
                treeItem.setId(next.getMenuid());
                treeItem.setText(next.getName());
                rootNode = treeItem.getId();
                iterator.remove();
            }
        }
        if (!CollectionUtils.isEmpty(list)) {
            List<TreeItem> trees = getChildren(rootNode, list);
            treeItem.setChildern(trees);
        }
        treeItems.add(treeItem);

        Object o = JSON.toJSON(treeItems);
        logger.info(String.valueOf(o));


    }

    private static List<TreeItem> getChildren(String rootNode, List<Menu> list) {
        List<TreeItem> treeItems = new ArrayList<>();
        for (Menu menu : list) {
            String parentid = menu.getParentid();
            String menuid = menu.getMenuid();
            TreeItem treeItem = new TreeItem();
            if (rootNode.equals(parentid)) {
                treeItem.setId(menu.getMenuid());
                treeItem.setText(menu.getName());
                List<TreeItem> children = getChildren(menuid, list);
                treeItem.setChildern(children);
                treeItems.add(treeItem);
            }

        }
        return treeItems;
    }

}
